package com.situ.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.situ.admin.bean.APIResult;
import com.situ.admin.bean.User;
import com.situ.admin.service.UserService;

@RestController
@RequestMapping("/api/users")
@CrossOrigin(origins = { "*" })
public class UserController {

	@Autowired
	private UserService userService;

	@GetMapping
	public APIResult get(Integer page, Integer limit, User user) {
		PageInfo pageInfo = userService.getByPage(page, limit, user);
		if (pageInfo == null || pageInfo.getTotal() == 0) {
			return APIResult.notFound("没有查询到任何数据");
		} else {
			return APIResult.ok(pageInfo);
		}
	}

	@DeleteMapping
	public APIResult remove(Integer id) {
		User user = userService.remove(id);

		if (user != null) {
			return APIResult.ok(user);
		} else {
			return APIResult.notFound("要删除的资源不存在");
		}

	}
}
